<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html dir="ltr"><head><title>Using Classless Scripts</title>


<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<xml></xml>
<link rel="stylesheet" type="text/css" href="MSDN.css"></head><body id="divID" class="dtBODY">
<div id="nsbanner">
<div id="bannerrow1">
<table class="bannerparthead" cellspacing="0">
<tbody>
<tr id="hdr">
<td class="runninghead">
<p align="left">CS-Script 3.27.0</p>
</td>
<td class="product"> <br>
</td>
</tr>
</tbody>
</table>
</div>
<div id="TitleRow">
<h1 class="dtH1" align="left">Using Classless Scripts</h1>
</div>
</div>
<br>
<div id="nstext"><font face="Arial" size="2"><span><span style="font-style: italic;"></span><span style="font-weight: bold;">Background</span><br>
Some CS-Script users&nbsp;asked about execution of the classless
scripts.<br>
<br>
</span></font>
<div style="margin-left: 40px;"><span style="font-style: italic; color: rgb(0, 0, 153);">...I know
many other scripting languages (JS, python, etc) don't require class </span><span style="font-style: italic; color: rgb(0, 0, 153);">and
function declarations, yet still allow them if so desired.
&nbsp;Could </span><span style="font-style: italic;"><span style="color: rgb(0, 0, 153);">your engine do the same?...<br>
</span><br>
</span></div>
<font face="Arial" size="2"><span>There is a
big difference between Python/JS and CS-Script as development systems.
CS-Script is entirely based on the existing language (ECMA C#). In a
way CS-Script is an extension&nbsp;of the C# and as such it follows
all C# and CLR patterns and constraints. This</span></font><font face="Arial" size="2"><span> approach</span></font><font face="Arial" size="2"><span> offers a lot of
benefits:</span></font><br>
<div>
<ul>
<li><font face="Arial" size="2"><span><span>no
need to develop any special C# compiler</span></span></font>
</li>
<li><font face="Arial" size="2"><span><span></span></span></font><font face="Arial" size="2"><span>script code is
interchangeable (any existing usual C# code&nbsp;can be taken and
executed as a script)</span></font> </li>
<li><font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span>compiled scripts
can be referenced as&nbsp;class libraries (because they are) by
other CLR modules</span></font> </li>
<li><font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span>developers do not
need to remember any syntactical differences between C# and C# script
(because </span></font><font face="Arial" size="2"><span>there are none)</span></font>
</li>
<li><font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span>very small
footprint due to usage of existing compilers</span></font> </li>
<li><font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span><span>no need
to develop any new IDEs (any existing&nbsp;IDE for C#&nbsp;can
be used)</span></span></font> </li>
<li><font face="Arial" size="2"><span><span>......<br>
</span></span></font></li>
</ul>
</div>
<font face="Arial" size="2"><span>Of course
it is possible&nbsp;for the script engine to&nbsp;execute <span class="st0" id="st" name="st">classless</span>
(free-style) C# code.&nbsp;Even&nbsp;very
simple text manipulation&nbsp;with the classless C# code (inserting
the
code in the empty&nbsp;class declaration) allows to create a
standard C#
code and execute it. However it would ultimately
require&nbsp;creating and
maintaining specially designed IDE and/or code editors, special
compiler message translators etc. With classless
scripts no third-party tools can be used&nbsp;to assists
with&nbsp;development. </span></font><font face="Arial" size="2"><span>In other
words,&nbsp;all benefits listed above would be diminished.<br><br>
</span></font><font face="Arial" size="2"><span>Having
said&nbsp;that, it is impossible to ignore the fact that in some
situations it would be desirable to have very light scripts with
working code only and no code infrastructure. Fortunately, CS-Script
offers automatic generation of the wrapper class if the script does not
have any class definition. Classless support comes in a few different
flavors, which should not be confused:<br><br><a style="font-style: italic;" href="AutoClass.html"><span style="font-weight: bold;">Execution of classless script files with command line switch /autoclass.</span></a><br>This feature is implemented in the CS-Script engine itself. It is to be used with stand alone scripts:<br>&nbsp;- User cannot control the way auto-class is generated.<br>&nbsp;- Script must have entry point Main().<br><br>
<span style="font-style: italic;">Example:</span><br></span></font><table style="text-align: left; width: 773px; height: 108px;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(255, 255, 204); white-space: nowrap;"><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System;<br><br><font color="#0000ff" size="2">void</font>&nbsp;Main()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(greeting);<br>}<br></font></td></tr></tbody></table><br style="font-style: italic;"><font face="Arial" size="2"><span><span style="font-style: italic;">It
is implemented as an internal precompiler thus it can be easily
improved by developing your custom "autoclass precompiler" instead. See
<a href="precompilers.html">Precompilers</a> chapter for details.<br>
</span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;">The details can be found </span><a style="font-style: italic;" href="AutoClass.html">here</a><span style="font-style: italic;">.</span><br><br><a style="font-style: italic;" href="Script_hosting_guideline_.html"><span style="font-weight: bold;">Execution of classless C# code in script hosting scenarios.</span></a><br>This feature is implemented in the CS-Script engine class library (<span style="font-style: italic;">CSScriptLibrary.dll</span>). It is to be used with C# code dynamically executed from the host application:<br>&nbsp;- User cannot control the way auto-class is generated.<br>&nbsp;- Script is a C# code in memory (not a file).<br style="font-style: italic;"><br>&nbsp;<span style="font-style: italic;">Example:</span><br></span></font><table style="text-align: left; width: 779px; height: 156px;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(255, 255, 204); white-space: nowrap;"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2"><font color="#0000ff" size="2">var</font>&nbsp;code&nbsp;= @<font color="#800000" size="2">"public&nbsp;static&nbsp;void&nbsp;Hello(string&nbsp;greeting)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(greeting);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}"</font>;<br><br><font color="#0000ff" size="2">var</font>&nbsp;SayHello&nbsp;=&nbsp;</font><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2"><font color="#0000ff" size="2">new</font>&nbsp;AsmHelper(CSScript.LoadMethod(code))<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; .</font></span></font></font></span></font></font></span></font></font></span></font><font face="Courier New" size="2">GetStaticMethod(); <br><br><font color="#008000" size="2"></font>SayHello(<font color="#800000" size="2">"Hello&nbsp;World!"</font>);</font></span></font></font></span></font></font></span></font></font></span></font></td></tr></tbody></table><font face="Arial" size="2"><span><span style="font-style: italic;"><br>The details can be found&nbsp;</span><a style="font-style: italic;" href="Script_hosting_guideline_.html">here</a><span style="font-style: italic;">.</span><br><br><a href="Classless.html"><span style="font-weight: bold; font-style: italic;">Execution of classless script files with Alternative Compiler.</span></a><br>This feature is implemented in the CS-Script engine extension library (<a href="Alternative_Compilers.html"><span style="font-style: italic;">CSSCodeProvider.dll</span></a>). It is to be used with both hosted and standalone scripts:<br>&nbsp;- Scripts must have .ccs extension to be handled as classless.<br>&nbsp;- User can control the way auto-class is generated (namespace and class name) by using //css_classless directive.<br><br><span style="font-style: italic;"></span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;">Hosting scenario e</span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;">xample &nbsp;- Client (script.ccs)<br></span></span></font><table style="text-align: left; width: 781px; height: 124px;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(255, 255, 204); white-space: nowrap;"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2"><font color="#008000" size="2">//css_classless&nbsp;MyNamespace.MyClass;</font></font></span></font></font></span></font></font></span></font></font></span></font><br><font face="Courier New" size="2"><font color="#0000ff" size="2">using</font>&nbsp;System;<br></font><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2"><br><font color="#0000ff" size="2">static</font>&nbsp;<font color="#0000ff" size="2">public</font>&nbsp;<font color="#0000ff" size="2">void</font>&nbsp;SayHello(<font color="#0000ff" size="2">string</font>&nbsp;greeting)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(greeting);<br>}<br></font><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span></span></font></font></span></font></font></span></font></font></span></font></span></font></font></span></font></font></span></font></font></span></font><font face="Courier New" size="2"></font></td></tr></tbody></table><br><font face="Arial" size="2"><span><br><span style="font-style: italic;"></span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;">Hosting scenario e</span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;">xample &nbsp;- </span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;">Host</span></span></font><font face="Arial" size="2"><span><span style="font-style: italic;"><br></span></span></font><table style="text-align: left; width: 780px; height: 124px;" border="1" cellpadding="2" cellspacing="2">
<tbody>
<tr>
<td style="background-color: rgb(255, 255, 204); white-space: nowrap;"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2"><font color="#008000" size="2"></font></font></span></font></font></span></font></font></span></font></font></span></font><font face="Courier New" size="2">...<br></font><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2">CSScript.GlobalSettings.UseAlternativeCompiler&nbsp;=<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Environment.ExpandEnvironmentVariables(@<font color="#800000" size="2">"%CSSCRIPT_DIR%\Lib\CSSCodeProvider.dll"</font>);<br><br><font color="#0000ff" size="2">var</font>&nbsp;helper&nbsp;=&nbsp;<font color="#0000ff" size="2">new</font>&nbsp;AsmHelper(CSScript.Load(<font color="#800000" size="2">"script.ccs"</font>));<br>helper.Invoke(<font color="#800000" size="2">"MyNamespace.MyClass.SayHello"</font>,&nbsp;<font color="#800000" size="2">"Hello&nbsp;World!"</font>);</font></span></font></font></span></font></font></span></font></font></span></font><br><font face="Courier New" size="2">...</font><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font face="Courier New" size="2"><br></font><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span><font size="4"><font size="2"><span></span></font></font></span></font></font></span></font></font></span></font></span></font></font></span></font></font></span></font></font></span></font></td></tr></tbody></table><font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><span><br><span style="font-style: italic;">The details can be found </span><a style="font-style: italic;" href="Classless.html">here</a><span style="font-style: italic;">.</span><br><br>For
the wast majority of scripting scenarios the built-in support for
classless scripting is sufficient. If you need more control on
auto-class being generated you can use CSSCodeProvider.dll distributed
along with the CS-Script binaries.<br><br>However if you want ultimate
flexibility you can implement Alternative Compiler, which can be
considered as a "precompiler" doing some source code
manipulations/injections prior the actual compilation with the normal
C# compiler. You can find the tutorial for implementing Alternative
Compilers <a href="non_cs_compilers.html">here</a>.<br><br>Standard CS-Script installation contains examples of classless scripts in the <span style="font-style: italic;">&lt;cs-script&gt;\Samples\Classless</span> directory.<br></span></font><font face="Arial" size="2"><span></span></font><br>
<h4 class="dtH4">See Also</h4>
<br><font face="Arial" size="2"><span><a href="Script_hosting_guideline_.html">Autoclass</a><span style="font-style: italic;"></span></span></font>| <font face="Arial" size="2"><span></span></font><font face="Arial" size="2"><a href="Script_hosting_guideline_.html">Script hosting guideline</a></font>&nbsp;|&nbsp;<a href="Importing_scripts.html"><font face="Arial" size="2"><span></span></font><font face="Arial" size="2"></font></a><font face="Arial" size="2"><a href="Classless.html">Alternative Compilers</a></font><font face="Arial" size="2"><a href="Classless.html"><span style="font-weight: bold; font-style: italic;"></span></a></font><br><br><a href="ws.html"><span style="text-decoration: underline;">
</span></a></div>
</body></html>